package com.saikuo.service.dao.impl;

import com.saikuo.service.constant.OrderStatus;
import com.saikuo.service.dao.IPlatformDao;
import com.saikuo.service.model.Order;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by shanchangyu on 16/10/18.
 */
@Repository
public class PlatformDaoImpl extends JdbcDaoSupport implements IPlatformDao {
    private static final BeanPropertyRowMapper<Order> mapper =
            new BeanPropertyRowMapper<>(Order.class);

    @Autowired
    PlatformDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }

    private static final String UPDATE_ORDER = " update y_order set status =?,update_time=? where order_id=?";

    @Override
    public boolean updateOrderInfo(Long orderId, int status) {
        return 1 == this.getJdbcTemplate().update(UPDATE_ORDER, new Object[]{status, new Date(), orderId});
    }

    private static final String GET_ORDER_INFO_BY_USER_ID = "select * from y_order where store_code=? and status=? order by update_time desc limit ?,?";

    @Override
    public List<Order> getOrdersByStoreCode(int storeCode, int orderStatus, int from, int limit) {
        List<Order> result = this.getJdbcTemplate().query(GET_ORDER_INFO_BY_USER_ID,
                new Object[]{storeCode, orderStatus, from, limit}, mapper);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result;
    }

}
